package com.gaojinqi.sys.modular.rbac.controller;

import com.gaojinqi.sys.core.auth.util.LoginContext;
import com.gaojinqi.sys.core.auth.util.LoginContextHolder;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import static com.gaojinqi.base.common.constant.ViewConst.REDIRECT;
import static com.gaojinqi.base.common.constant.ViewConst.TIPS_KEY;

/**
 * @author gaojinqi
 * @version 1.0
 * @since 2020年05月08日
 */
@Api(tags = "登录页面")
@Controller
@Slf4j
public class LoginPageController {

    @GetMapping("/")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        if (LoginContextHolder.getContext().hasLogin()) {
            modelAndView.addObject("user", LoginContextHolder.getContext().getUser());
            modelAndView.setViewName("/index");
        } else {
            modelAndView.setViewName("/login-page");
        }
        return modelAndView;
    }

    @GetMapping("/loginPage")
    public ModelAndView loginPage() {
        ModelAndView modelAndView = new ModelAndView();
        if (LoginContextHolder.getContext().hasLogin()) {
            modelAndView.setViewName(REDIRECT + "/");
        } else {
            modelAndView.addObject(TIPS_KEY, "请重新登录");
            modelAndView.setViewName("/login-page");
        }
        return modelAndView;
    }

}
